const { path, fs, selectedFileName, rootPath, getMatchingFolderPath } = require('../scripts/common.js');

// 右侧标签(能力点/原材料/方法库)相关功能
function showAttributesByType(type) {
    let data;
    try {
        const matchingFolderPath = getMatchingFolderPath();
        if (!matchingFolderPath) {
            throw new Error('找不到匹配的文件夹');
        }

        // 获取当前已加载的 skillpoint 数据
        const skillpointPath = path.join(matchingFolderPath, 'skillpoint.json');
        const content = fs.readFileSync(skillpointPath, 'utf8');
        data = JSON.parse(content);

        let typeKey;
        switch (type) {
            case '能力点': typeKey = 'ability'; break;
            case '原材料': typeKey = 'material'; break;
            case '方法库': typeKey = 'method'; break;
        }

        // 只筛选和更新第四区域的内容，不影响其他区域
        const attributes = data.attributes.filter(attr => attr.type === typeKey);
        const card4 = document.querySelector('.card-4');
        card4.innerHTML = `
            <div class="attributes-container">
                <div class="card-title">${type}</div>
                <div class="card-list"></div>
            </div>
        `;
        
        const cardList = card4.querySelector('.card-list');

        if (attributes.length === 0) {
            cardList.innerHTML = `<div class="attribute-card">
                <div class="card-title">暂无${type}数据</div>
            </div>`;
            return;
        }

        attributes.forEach(attr => {
            const card = document.createElement('div');
            card.className = 'attribute-card';
            
            let title = '';
            let content = '';

            switch (type) {
                case '能力点':
                    title = `${attr.name}: ${attr.currentValue || attr.defaultValue || 0}`;
                    content = attr.detail || '';
                    break;

                case '原材料':
                    title = `${attr.name}: ${attr.currentValue || attr.defaultValue || 0}${attr.unit || ''}`;
                    content = attr.detail || '';
                    break;

                case '方法库':
                    title = attr.name;
                    content = formatMethodContent(attr);
                    break;
            }

            card.innerHTML = `
                <div class="card-title">${title}</div>
                ${content ? `<div class="card-content">${content}</div>` : ''}
            `;
            cardList.appendChild(card);
        });

    } catch (error) {
        console.error('Error loading skillpoint data:', error);
        const container = document.querySelector('.card-4');
        container.innerHTML = '加载属性失败';
    }
}

function formatMethodContent(attr) {
    let content = '';
    
    if (attr.detail) {
        content += `<div class="method-section"><strong>详情：</strong>${attr.detail}</div>`;
    }
    
    if (attr.scope) {
        content += `<div class="method-section"><strong>适用范围：</strong>${attr.scope}</div>`;
    }
    
    if (attr.amplification && attr.amplification.length > 0) {
        content += `<div class="method-section">
            <strong>增幅方式：</strong>
            <ul>${attr.amplification.map(item => `<li>${item}</li>`).join('')}</ul>
        </div>`;
    }
    
    if (attr.notes && attr.notes.length > 0) {
        content += `<div class="method-section">
            <strong>注意事项：</strong>
            <ul>${attr.notes.map(item => `<li>${item}</li>`).join('')}</ul>
        </div>`;
    }
    
    return content;
}

// 导出函数
module.exports = {
    showAttributesByType
}; 